package com.neulogistics.core.substation.domain.model.enumeration;

import com.neulogistics.core.substation.domain.exception.SubstationDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class PaymentType extends Enumeration {

    public static final int Crediting_Id = 0;
    public static final int Refund_Id = 1;

    public static PaymentType Crediting = new PaymentType(Crediting_Id, "Crediting");
    public static PaymentType Refund = new PaymentType(Refund_Id, "Refund");


    protected PaymentType(int id, String name) {
        super(id, name);
    }

    private static List<PaymentType> list = Arrays.asList(Crediting, Refund);

    public static List<PaymentType> getList(){return list;}

    public static PaymentType fromName(String name) throws SubstationDomainException {
        Optional<PaymentType> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()){
            return optional.get();
        }
        else {
            throw new SubstationDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            PaymentType.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}
